<?php
/*
     CREATE TABLE `news` (
	`id` INT(10) NOT NULL,
	`date` DATE NOT NULL,
	`user_id` INT(10) NOT NULL,
	`status` INT(1) NULL DEFAULT NULL,
	`source_author` VARCHAR(50) NULL DEFAULT NULL,
	`source_name` VARCHAR(255) NULL DEFAULT NULL,
	`source_url` VARCHAR(255) NULL DEFAULT NULL,
	`source_date` DATETIME NULL DEFAULT NULL,
	`subject` VARCHAR(255) NOT NULL,
	`headline` VARCHAR(255) NOT NULL,
	`content` TEXT NULL,
	PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
     */
    
class News_model extends CI_Model {
    protected $_tableNews = "news";
    protected $_tableUsers = "users";
    function __construct() {
        parent::__construct();
	$this->load->database();
    } 
    public function _substr($str, $length, $minword = 3)
        {
            $sub = '';
            $len = 0;

            foreach (explode(' ', $str) as $word)
            {
                $part = (($sub != '') ? ' ' : '') . $word;
                $sub .= $part;
                $len += strlen($part);

                if (strlen($word) > $minword && strlen($sub) >= $length)
                {
                    break;
                }
            }

            return $sub . (($len < strlen($str)) ? '...' : '');
        }
        
    public function LoadUserNameByUserId($user_id){
        
        $this->db->select('username');        
        $this->db->where("id",$user_id);
        $query = $this->db->get($this->_tableUsers);          
          if($query)
              return $query->row_array();
          else
              return FALSE;
    }
   // News     
   public function loadNewsHomeLatest(){
        $this->db->select('id,headline,subject');
        $this->db->from('news');
        $this->db->where('status', '1');
        $this->db->order_by("id", "desc");
        $this->db->limit(1);
        $query = $this->db->get();       
        return $query->row_array();
    }
    public function loadNewsLatestList($offset){
         $this->db->limit($offset);
         $this->db->where('status', '1');
         $this->db->order_by("id","desc");
         return $this->db->get('news')->result_array();
     }   
    
    // Item
    public function loadNewsItemInfo($id = 0){
           
            $query_items = $this->db->get_where("news",array('id'=>$id));
            return $query_items->row_array();		  
     }
}
?>